﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TT.Module.sysLogin;
using Exp.BillDLL.dao;
using TT.ORM;
using System.Data;
using Bill.BasDLL.ety;

namespace Exp.BillDLL.biz
{
    public class bizExpBill
    {
        etyBaeSystemParamter basSysParam;
        daoExpBill doExpBill;

        public bizExpBill(etyBaeSystemParamter ieyBasClassDeclare)
        {
            basSysParam = ieyBasClassDeclare;
            doExpBill = new daoExpBill(basSysParam);
        }

        public IList<TT.ORM.etyCodeRef> GetCodeRef(TT.ORM.etyCodeRef inEty)
        {
            DataTable dt = doExpBill.GetCodeRef(inEty);
            IList<TT.ORM.etyCodeRef> ilRusult = TT.Common.Converts.ConvertToIList<TT.ORM.etyCodeRef>(dt);
            return ilRusult;
        }

        /// <summary>
        /// 获取基础数据结构一致的表
        /// </summary>
        /// <param name="iTableName"></param>
        /// <param name="iColumnOrderIdx"></param>
        /// <returns></returns>
        public IList<etyBaseTable> GetBaseTableCodeName(string iTableName, int iColumnOrderIdx,bool isInsertAllItem)
        {
            string sFields = string.Empty;
            string sTableName = iTableName;
            string sWhere = string.Empty;
            switch (iTableName.ToUpper())
            {
                case "B_SF":
                case "B_YW_TYPE":
                case "B_CITY":  
                case "B_QX":  
                    sFields = "X_CODE,X_CNAME,X_ENAME";
                    break;
                case "ACC_POP":
                    sTableName = "CODE_REF";
                    sWhere = " where item='ACC_POP' ";
                    sFields = "VALUE X_ID,CODE X_CODE,CODE_DESC X_NAME";
                    break;
                case "BILL_USE_MOD":
                    sTableName = "CODE_REF";
                    sWhere = " where item='BILL_USE_MOD' ";
                    sFields = "VALUE X_ID,CODE X_CODE,CODE_DESC X_NAME";
                    break;
                case "C_COUNTRY_REGION":
                case "C_DOM_REGION":
                case "C_FOR_PORT":
                    sFields = "X_CODE,X_CNAME X_NAME";
                    break;
                case "T_DOMESTIC_PORT":
                    sFields = "PORT_CODE X_CODE,PORT_CNAME X_NAME";
                    break;
                case "T_DOMESTIC_PORT_FILTER_COMBO":
                    sTableName = "T_DOMESTIC_PORT";
                    sFields = "PORT_CODE X_CODE,PORT_CNAME X_NAME";
                    sWhere = " where port_code in(SELECT distinct ENTRY_PORT_CODE FROM V_DeclDocList where ENTRY_PORT_CODE is not null) ";
                    break;
                case "SM_EMAIL_ADDR":
                    sFields = "ORG_CODE X_CODE,ORG_NAME X_NAME";
                    break;
                case "T_ORG_INFO":
                    sFields = "ORG_CODE X_CODE,ORG_CNAME X_NAME";
                    break;
                case "CIQS_T_CUSTOM_INFO":
                    sFields = "CUSTOM_CODE X_CODE,CUSTOM_NAME X_NAME";
                    break;
                case "T_PACK_TYPE":
                    sFields = "PACK_TYPE_CODE X_CODE,PACK_CNAME X_NAME";
                    break;
                case "UNIT":
                    sFields = "UNIT X_CODE,UT_NAME X_NAME";
                    break;
                case "FEE_TYPE_BSE":
                    sFields = "FEE_TYPE X_CODE,FEE_NAME X_NAME,trim(to_char(FEE_TYPEID)) X_ID";
                    sWhere = " where attribute in('0','1') ";
                    break;
                case "FEE_TYPE_BSE_INTL":
                    sTableName = "FEE_TYPE_BSE";
                    sFields = "FEE_TYPE X_CODE,FEE_NAME X_NAME,trim(to_char(FEE_TYPEID)) X_ID";
                    sWhere = " where attribute in('0','2') ";
                    break;
                case "USERS":
                    sFields = "USER_ID X_CODE,ID X_ID,USER_NAME X_NAME";
                    break;
                case "CURR":
                    sFields = "CR_AB X_CODE,CR_NAME X_NAME,trim(to_char(CR_ID)) X_ID";
                    break;
                case "DECL_TYPE":
                    IList<etyBaseTable> ilBase = new List<etyBaseTable>();
                    etyBaseTable eyBase = new etyBaseTable();
                    eyBase.X_CODE = "1";
                    eyBase.X_NAME = "入境";
                    ilBase.Add(eyBase);
                    eyBase = new etyBaseTable();
                    eyBase.X_CODE = "2";
                    eyBase.X_NAME = "出境";
                    ilBase.Add(eyBase);
                    return ilBase;
                    break;
                case "GETACROSS":
                    IList<etyBaseTable> ilBase2 = new List<etyBaseTable>();
                    etyBaseTable eyBase2 = new etyBaseTable();
                    eyBase2.X_CODE = "1";
                    eyBase2.X_NAME = "放行";
                    ilBase2.Add(eyBase2);
                    eyBase2 = new etyBaseTable();
                    eyBase2.X_CODE = "0";
                    eyBase2.X_NAME = "未放行";
                    ilBase2.Add(eyBase2);
                    return ilBase2;
                    break;
                default:

                    break;
            }
            if (sFields.Trim() != "" && sFields.Split(',').Length < 3)
            {
                sFields = sFields + ",'' X_ID";
            }
            DataTable dt = doExpBill.GetBaseTableCodeName(sTableName, iColumnOrderIdx, sFields, sWhere);
            IList<etyBaseTable> ilRusult = TT.Common.Converts.ConvertToIList<etyBaseTable>(dt);
            if (isInsertAllItem)    //JustMe 20140428 add ***
            {
                etyBaseTable eyBas = new etyBaseTable();
                eyBas.X_ID = "-1";
                eyBas.X_CODE = "***";
                eyBas.X_NAME = "***";
                eyBas.X_CNAME = "***";
                ilRusult.Insert(0, eyBas);
            }
            return ilRusult;
        }

        public DataTable GetArea(etyArea inEty)
        {
            string sql = "CLIENT_NAME,BUSS_DESC,SF_DESC,CLIENT_ID,BUSS_TYPE,SF";

            IList<etyArea> ilEty = new List<etyArea>();
            ilEty.Add(inEty);
            return basSysParam.SqlHelp.ExecuteReader<etyArea>(sql, ilEty);
        }

        public IList<etyClient> GetClient()
        {
            DataTable dt = doExpBill.GetClient();
            IList<etyClient> ilRusult = TT.Common.Converts.ConvertToIList<etyClient>(dt);
            return ilRusult;
        }
    }
}
